package com.xu.blog_after_end.service;

import com.xu.blog_after_end.dao.BlogRepository;
import com.xu.blog_after_end.dao.CommentRepository;
import com.xu.blog_after_end.dao.StatisticsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    
    @Autowired
    private StatisticsRepository statisticsRepository;
    
    @Autowired
    private BlogRepository blogRepository;
    
    @Autowired
    private CommentRepository commentRepository;
    
    @Override
    public List<Map<String, Object>> getBlogPublishTrend() {
        return statisticsRepository.countBlogByMonth();
    }
    
    @Override
    public List<Map<String, Object>> getTypeStatistics() {
        return statisticsRepository.countBlogByType();
    }
    
    @Override
    public List<Map<String, Object>> getTagStatistics() {
        return statisticsRepository.countBlogByTag();
    }
    
    @Override
    public List<Map<String, Object>> getTopViewedBlogs() {
        return statisticsRepository.findTopViewedBlogs();
    }
    
    @Override
    public List<Map<String, Object>> getMostCommentedBlogs() {
        return statisticsRepository.findMostCommentedBlogs();
    }
    
    @Override
    public Map<String, Object> getOverallStatistics() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 博客总数
        long blogCount = blogRepository.count();
        statistics.put("blogCount", blogCount);
        
        // 评论总数
        long commentCount = commentRepository.count();
        statistics.put("commentCount", commentCount);
        
        // 访问总量（所有博客的访问量之和）
        Long totalViews = blogRepository.findAll().stream()
                .mapToLong(blog -> blog.getViews() != null ? blog.getViews() : 0)
                .sum();
        statistics.put("totalViews", totalViews);
        
        return statistics;
    }
}